/**
 * @FileName: Product.java
 * @Author
 * @Description:
 * @Date 2016年4月20日 下午3:38:57
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.model.product;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.util.InterfaceUtil;

public class Product extends Model<Product>{
    private static final long   serialVersionUID = -3752786594230267176L;
    public static final Product dao              = new Product();

    public static String        NAME             = "name";
    public static String        TITLE            = "title";
    public static String        DESC_SUMMARY     = "desc_summary";
    public static String        DESC_AFTERSALE   = "desc_aftersale";

    public static String        ID               = "id";
    public static String        BRAND_ID         = "brand_id";
    public static String        CATE_ID          = "cate_id";
    public static String        THUMBNAIL        = "thumbnail";
    public static String        BANNER_IMG       = "banner_img";
    public static String        SORT             = "sort";
    public static String        PROFIT           = "profit";
    public static String        DESP_SUMMARY     = "desp_summary";
    public static String        DESP_AFTERSALE   = "desp_aftersale";
    public static String        DESP_DETAIL      = "desp_detail";
    public static String        PUT_ON_TIME      = "put_on_time";
    public static String        PUT_OFF_TIME     = "put_off_time";
    public static String        OPERATED_BY      = "operated_by";
    public static String        OPERATED_TIME    = "operated_time";
    public static String        VISIT            = "visit";
    public static String        DEPOT_ID         = "depot_id";
    public static String        IS_VISIBLE       = "is_visible";
    public static String        STATUS           = "status";
    public static String        ORG_ID           = "org_id";
    public static String        CREATOR          = "creator";
    public static String        CREATE_TIME      = "create_time";
    public static String        OPERATED_DESC    = "operated_desc";
    public static String        ATTR_TYPE        = "attr_type";
    public static String        ENABLED          = "enabled";

    /**
     * 获取泛店员终端销售可授权业务数据
     * 
     * @Title: getProductList
     * @Description:条件：有效(is_visible=1) + 已启用(enabled=1) + 上架时间有效期内 + 只取自己所在地市数据
     *                                  + 机型编码不能为空 + 商品对应的sku信息有效(enabled=1)
     * @return List<Record>
     */
    public List<Record> getProductList(){
        int orgId = InterfaceUtil.getOrgId();

        String sql = "SELECT p.id, p.name FROM product p LEFT JOIN product_region pr ON pr.product_id = p.id LEFT JOIN product_sku ps ON ps.product_id = p.id WHERE p.is_visible = 1 AND p.enabled = 1 AND ( p.put_on_time <= NOW() AND (( p.put_off_time IS NOT NULL AND p.put_off_time >= NOW()) OR p.put_off_time IS NULL )) AND ps.enabled = 1 AND ps.model_code IS NOT NULL AND ps.model_code <> '' AND pr.region_id = ( SELECT o.CityCode FROM organization o WHERE o.id = ? ) GROUP BY p.id ORDER BY p.create_time DESC";

        return Db.find(sql, orgId);
    }

    /**
     * @Title: getSqlExceptSelect
     * @Description:泛店员获取已授权列表时组装sql脚本
     * @return String
     */
    public String getSqlExceptSelect(){
        return "product bus_temp ON bac.config_business_id = bus_temp.id LEFT JOIN product_sku ps ON ps.product_id = bus_temp.id WHERE bus_temp.is_visible = 1 AND bus_temp.enabled = 1 AND ( bus_temp.put_on_time <= NOW() AND (( bus_temp.put_off_time IS NOT NULL AND bus_temp.put_off_time >= NOW()) OR bus_temp.put_off_time IS NULL )) AND ps.enabled = 1 AND ps.model_code IS NOT NULL AND ps.model_code <> '' ";
    }

}
